<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>档案预览</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/layer-css.css"/>
    <style>
        .organizationTree {
            width: 100% !important;
            height: -webkit-calc(100vh - 160px);
            height: -moz-calc(100vh - 160px);
            height: calc(100vh - 112px);

        }

        .centered {
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .separateInTheCenter {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
        }

        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }

        body {
            font-family: Source Han Sans SC, Source Han Sans SC;
        }

        .searchCriteria .layui-form-label {
            width: 64px;
            font-size: 16px;
            color: #5A6878;
        }

        .searchCriteria button {
            line-height: 31px;
            font-size: 16px;
        }

        .searchCriteria .layui-icon {
            font-size: 15px;
        }

        .toolbarButton button {
            font-size: 16px;
            border: 0px;
            border-radius: 5px 5px 5px 5px;
        }

        .toolbarButton .layui-btn {
            margin-bottom: 0px;
        }

        .layui-table-box button {
            font-size: 14px !important;
            height: 25px;
            line-height: 23px;
        }

        #moreTools a {
            color: #1890FF;
            margin-left: 20px;
        }

        .layui-table-checked {
            background-color: #9e9e9e14;
        }

        .layui-table-checked.layui-table-hover {
            background-color: #9e9e9e14 !important;
        }

        .layui-btn-container .layui-btn {
            margin-left: 15px !important;
        }

        .toolbarButton button {
            font-size: 16px;
            border: 0px;
            border-radius: 5px 5px 5px 5px;
        }

        .dree-toolbar .layui-btn {
            font-size: 14px;
            width: 46px;
            height: 26px;
            line-height: 26px;
            padding: 0px;
        }

        .dtree-theme-item:hover cite {
            color: #398FFF !important;
        }

        .dtree-toolbar-fixed a i {
            font-size: 16px;
            color: #03A9F4;
        }

        .dtree-toolbar-fixed a {
            margin-right: 5px;
        }

    </style>
</head>
<body class="pear-container">
<div class="layui-row layui-col-space15">
    <div class="layui-col-sm12 layui-col-md2">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="toolbarButton dree-toolbar centered" style="justify-content: space-around;">
                    <div style="font-size: 16px;">区域管理</div>
                    <button class="layui-btn layui-btn-primary layui-border-green"
                            style="background-color: rgb(57, 143, 255, 0.1);color: #398FFF !important;"
                            lay-on="addTree">新增
                    </button>
                    <button class="layui-btn layui-btn-primary layui-border-green"
                            style="background-color: #F2F2F2;color: #969FAB !important;margin-left: 0px;"
                            lay-on="removeTree">删除
                    </button>
                </div>
                <div style="color: #999 !important;padding-left: 12px;font-size: 12px;">鼠标右击编辑区域</div>
                <div id="organizationTreeContent" style="overflow: auto;padding-top: 10px;">
                    <ul id="organizationTree" class="dtree organizationTree" data-id="9527"></ul>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-sm12 layui-col-md10">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="area-table" lay-filter="area-table"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="host-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">修改</button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">删除</button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dtree', 'util', 'treetable'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;
            let dtree = layui.dtree;
            var util = layui.util;
            var areaId = "";
            var areaName = "";
            let treetable = layui.treetable;

            var DTree1 = dtree.render({
                elem: "#organizationTree",
                initLevel: "2", //默认展开层级为1
                line: true, // 有线树
                ficon: ["2", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
                icon: ["-1", "-1"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
                method: 'get',
                url: "/archives/area/buildAreaTree",
                // checkbar:true,
                skin: "laySimple",  // laySimple主题风格
                toolbar: true,
                record: true,
                toolbarWay: "fixed",
                // toolbarShow: ["edit","pulldown","pullup"],  // 这样就只会出现一个编辑页
                toolbarShow: [],
                toolbarExt: [
                    {
                        toolbarId: "editBtn",
                        icon: "dtree-icon-bianji",
                        title: "编辑区域",
                        handler: function (node, $div) {
                            console.log(node)
                            parent.layer.open({
                                title: ['修改 <span style="color: #ff5722;"></span> ' + node.context + ' 区域', 'font-size: 16px;'],
                                type: 2,
                                offset: 'r',
                                anim: 'slideLeft', // 从右往左
                                area: ['40%', '100%'],
                                shade: 0.5,
                                maxmin: true,
                                id: 'addNewEquipment',
                                content: '/archives/area/update/' + node.nodeId,
                                move: false,
                                end: function () {
                                    dtree.reload("organizationTree");
                                }
                            });
                        }
                    },]
            });

            // 区域树节点点击事件
            dtree.on("node(organizationTree)", function (obj) {
                areaName = obj.param.context;
                treetable.search('#area-table', areaName);
                areaId = obj.param.nodeId;
                return false;
            });

            util.on({
                addTree: function () {
                    parent.layer.open({
                        title: ['新增区域', 'font-size: 18px;'],
                        type: 2,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.5,
                        area: ['40%', '100%'],
                        content: '/archives/area/add',
                        shadeClose: false,
                        move: false,
                        end: function () {
                            dtree.reload("organizationTree");
                        }
                    });
                },
                removeTree: function () {
                    if (areaId === "") {
                        layer.msg("请点击选择区域", {icon: 2, time: 1000});
                        return false;
                    } else {
                        layer.confirm('<div>确定要删除&nbsp;&nbsp;<span style="color: #FF9200;">' + areaName +
                            '</span>&nbsp;&nbsp;区域吗?</div>', {
                            icon: 3,
                            title: ['删除确认', 'font-size: 18px;'],
                            shade: 0.5,
                            area: ['499px', '303px'],
                            move: false,
                            id: "borrowingConfirm"
                        }, function (index) {
                            layer.close(index);
                            $.ajax({
                                url: '/archives/area/has/child/' + areaId,
                                dataType: 'json',
                                type: 'get',
                                contentType: 'application/json',
                                success: function (result) {
                                    if (result.code === 200) {
                                        let loading = layer.load();
                                        $.ajax({
                                            url: '/archives/area/delete/save/' + areaId,
                                            dataType: 'json',
                                            contentType: "application/json;charset=utf-8",
                                            type: 'delete',
                                            success: function (result) {
                                                layer.close(loading);
                                                if (result.code === 200) {
                                                    layer.msg(result.message, {icon: 1, time: 1800}, function () {
                                                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                                                        location.reload();
                                                    });
                                                } else {
                                                    layer.msg(result.message, {icon: 2, time: 1800});
                                                }
                                            }
                                        })
                                    } else {
                                        layer.msg(result.message, {icon: 2, time: 1800});
                                    }
                                }
                            })
                        });
                    }
                },
                addDenseRack: function () {

                }
            });

            treetable.render({
                page: true,
                treeColIndex: 1,//树形图标显示在第几列
                treeSpid: 0,//最上级的父级id
                treeIdName: 'id', //id字段的名称
                treePidName: 'parentId',//父级节点字段
                // method:'post',
                skin: 'line',
                treeDefaultClose: true, //是否默认折叠
                // treeLinkage: false, //是否开启级联
                toolbar: '#type-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports'],
                elem: '#area-table',
                url: '/archives/area/list',
                id: '#area-table',
                height: 'full-30',
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'areaName', title: '区域名称'},
                        {field: 'sort', title: '排序', align: 'center'},
                        {field: 'status', title: '是否可用', templet: '#type-status', align: 'center'},
                        {field: 'createTime', title: '创建时间', align: 'center'},
                        {field: 'updateTime', title: '更新时间', align: 'center'},
                    ]
                ]
            });

            table.on('toolbar(area-table)', function (obj) {
                if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

        window.refresh = function (param) {
            treetable.reload('#area-table');
        }
        }
    )
</script>
</body>
</html>
